package com.photologics.main;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;

import com.photologics.logic.LogicHelper;
import com.photologics.logic.SelectSectionActivity;

public class SelectLogicActivity extends Activity {
	private String kakao_LogicName;
	private String reduce_KakaoLogicData;
	private String logicname;
	private String[] logic_name;
	private int[] logic_completeness;
	private String changeName;
	private int logicListColum;
	private long sameName;
	private TextView coin;

	@Override
	@SuppressWarnings("deprecation")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_select_logic);

		Intent kakaointent = getIntent();
		if (kakaointent.getStringExtra("kakaoName") != null) {

			kakao_LogicName = kakaointent.getStringExtra("kakaoName");
			reduce_KakaoLogicData = kakaointent.getStringExtra("kakaoData");

			sameName = LogicHelper.db.createLogic(kakao_LogicName,
					reduce_KakaoLogicData);

			if (sameName == -1) {
				showDialog(1);
			} else {
				Intent mintent = new Intent(SelectLogicActivity.this,
						SelectLogicActivity.class);
				mintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mintent);
			}

		}

		coin = (TextView) findViewById(R.id.select_logic_text_coin);
		coin.setText(LogicHelper.accountInfo.getCoin() + "");

		logic_name = LogicHelper.db.getNameList().clone();
		logic_completeness = LogicHelper.db.getCompletenessList().clone();

		View[] logic;
		ScrollView scrollview = (ScrollView) findViewById(R.id.scrollview_selectlogic);
		scrollview.setOverScrollMode(View.OVER_SCROLL_NEVER);

		int lcdWidth = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getWidth();

		GridLayout logicList = (GridLayout) findViewById(R.id.grid_selectlogic);

		logicListColum = 3;
		logicList.setColumnCount(logicListColum);

		if (logic_name.length == 0) {
			if (kakaointent.getStringExtra("kakaoName") == null)
				showDialog(2);
		} else {
			logic = new View[logic_name.length];

			for (int i = 0; i < logic_name.length; i++) {
				logic[i] = new View(this);
				logic[i] = new SelectLogic_item_Adapter(this, logic_name,
						logic_completeness).getView(i, null, logicList);
				logic[i].setPadding((lcdWidth / 4) / 3, 30, 0, 15);
				logic[i].setTag(logic_name[i]);

				logic[i].setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						logicname = v.getTag().toString();
						Intent intent = new Intent(SelectLogicActivity.this,
								SelectSectionActivity.class);
						intent.putExtra(LogicHelper.LOGIC_NAME_PARAMETER,
								logicname);
						startActivity(intent);
						Log.d("CLICK", "OnClickListener");
					}
				});

				logic[i].setOnLongClickListener(new View.OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						Log.d("CLICK", "OnLongClickListener");
						logicname = v.getTag().toString();
						showDialog(3);
						return true;
					}
				});
				logicList.addView(logic[i]);
			}
		}
	}

	@Override
	public void onBackPressed() {
		Intent it = new Intent(SelectLogicActivity.this, MainActivity.class);
		it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(it);
		finish();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 1:
			final Dialog namechange_Dialog = new Dialog(this,
					R.style.DialogTheme);
			View view1 = getLayoutInflater().inflate(
					R.layout.namechange_dialog, null);
			namechange_Dialog.setContentView(view1);
			namechange_Dialog.setCanceledOnTouchOutside(true);

			ImageButton same_ok;

			same_ok = (ImageButton) view1
					.findViewById(R.id.button_changename_ok);
			same_ok.setOnClickListener(new OnClickListener() {
				@SuppressWarnings("deprecation")
				@Override
				public void onClick(View v) {
					EditText editText = (EditText) namechange_Dialog
							.findViewById(R.id.EditText_namechange);
					editText.setSingleLine();
					int maxLength = 5;
					InputFilter[] filterArray = new InputFilter[1];
					filterArray[0] = new InputFilter.LengthFilter(maxLength);
					editText.setFilters(filterArray);
					// editText.setFilters(new InputFilter[] { new
					// InputFilter.LengthFilter(
					// 5) });
					changeName = editText.getText().toString();
					sameName = LogicHelper.db.createLogic(changeName,
							reduce_KakaoLogicData);
					if (sameName == -1)
						showDialog(1);
					else {
						Intent mintent = new Intent(SelectLogicActivity.this,
								SelectLogicActivity.class);
						mintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(mintent);

					}
				}
			});
			return namechange_Dialog;

		case 2:
			final Dialog null_logic_dialog = new Dialog(this,
					R.style.DialogTheme);
			View view2 = getLayoutInflater().inflate(
					R.layout.null_logic_dialog, null);
			null_logic_dialog.setContentView(view2);
			null_logic_dialog.setCanceledOnTouchOutside(true);

			ImageButton ok;

			ok = (ImageButton) view2.findViewById(R.id.button_null_ok);
			ok.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(SelectLogicActivity.this,
							MainActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
			});

			return null_logic_dialog;
		case 3:
			final Dialog kakao_delete_dialog = new Dialog(this,
					R.style.DialogTheme);
			View view3 = getLayoutInflater().inflate(
					R.layout.kakao_delete_dialog, null);
			kakao_delete_dialog.setContentView(view3);
			kakao_delete_dialog.setCanceledOnTouchOutside(true);

			Button delete;
			Button kakao;

			delete = (Button) view3.findViewById(R.id.dialog_delete);
			delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					LogicHelper.db.deleteLogic(logicname);
					Intent mit = new Intent(SelectLogicActivity.this,
							SelectLogicActivity.class);
					mit.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(mit);
				}
			});

			kakao = (Button) view3.findViewById(R.id.dialog_kakao);
			kakao.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					KakaoLink kakaoLink = KakaoLink
							.getLink(getApplicationContext());
					kakaoLink.invite(SelectLogicActivity.this, logicname);
					kakao_delete_dialog.dismiss();
				}
			});

			return kakao_delete_dialog;
		}
		return null;

	}
	protected void onPause() {
		LogicHelper.backgroundMusic.onPause(getApplicationContext());
		super.onPause();
	}

	@Override
	protected void onResume() {
		LogicHelper.backgroundMusic.onStart();
		super.onResume();
	}
}